package com.chenglu.android.cart.adapter;

import android.media.tv.TvView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.cart.CartFragment;
import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.common.utils.GlideUtils;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.goodsinfo.infoutils.GoodsInfoUtils;


import java.util.List;


/**
 * @author: lyj
 * @Created: by Copyright 2023/3/29 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：购物车布局
 */
public class CartAdapter extends BaseQuickAdapter<CartBean.Data, BaseViewHolder> {

    CartFragment cartFragment;

    public CartAdapter(CartFragment fragment, int layoutResId, List<CartBean.Data> data) {
        super(layoutResId, data);
        this.cartFragment = fragment;
    }

    @Override
    protected void convert(BaseViewHolder holder, CartBean.Data bean) {
        ImageView imageView = holder.getView(R.id.iv_module);
//        GlideUtils.setImageView(mContext,bean.);

        //    case none = 0                /// 空
        //    case shop = 1                /// 商超
        //    case restaurant = 2          /// 餐饮
        //    case beauty = 3              /// 美业
        //    case education = 4           /// 教育
        //    case minimart = 8            /// 便利店
        //    case pharmaceuticals = 9     /// 药店
        //    case breed = 10              /// 孕育
        //    case max = 999
        // / 平台公共
        TextView textView = holder.getView(R.id.tv_module_name);

        GoodsInfoUtils.setTypeShopsId(mContext,textView, bean.getShopsTypeId(),imageView);


        holder.setText(R.id.tv_cart_sellername, bean.getTenantName());


        holder.setChecked(R.id.cb_cart_seller, bean.isSellerCheck());
        holder.addOnClickListener(R.id.cb_cart_seller);

        holder.addOnClickListener(R.id.tv_cart_sellername);
        holder.addOnClickListener(R.id.tv_cart_settleaccounts);


//        cartFragment.setSum();
//        LogUtils.d("合计的值="+ cartFragment.mSum);
//        LogUtils.d("合计的值="+ bean.getTotalPrice());

        if(TextUtils.isEmpty(bean.getTotalAmount())){
            holder.setText(R.id.tv_check_number,"已选0件");
        }else {

            holder.setText(R.id.tv_check_number,"已选"+bean.getNumber()+"件");
        }

        if(TextUtils.isEmpty(bean.getTotalPrice())){
            holder.setText(R.id.tv_item_cartsum,"0.00");
        }else {
            holder.setText(R.id.tv_item_cartsum,bean.getTotalPrice());
        }

//        Glide.with(cartFragment).load(ContextCompat.getDrawable()).into(imageView);
        RecyclerView recyclerView = holder.getView(R.id.rv_cart_item);
        CartItemAdapter mItemAdapter = new CartItemAdapter(cartFragment, R.layout.item_carts_bean_slideview, bean.getShoppingTrolleyVOList());

        recyclerView.setLayoutManager(new LinearLayoutManager(cartFragment.getContext()));
        recyclerView.setAdapter(mItemAdapter);
//        mItemAdapter.setNewData(bean.getShopCartItems());
        mItemAdapter.notifyDataSetChanged();

        mItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CartBean.Data.ShoppingTrolleyVOList shopList = bean.getShoppingTrolleyVOList().get(position);

                switch (view.getId()) {
                    case R.id.rl_cart_minus:
                        cartFragment.cartUpdate(shopList.getId(),shopList.getGoodsId(), shopList.getGoodsAmount() - 1
                        ,shopList.getGoodsName(),shopList.getTenantId(),shopList.getShopsTypeId(),shopList.getIsSpec(),shopList.getStatus(),shopList.getGoodsSkuId());
                        break;
                    case R.id.rl_cart_add:
                        cartFragment.cartUpdate(shopList.getId(),shopList.getGoodsId(), shopList.getGoodsAmount() + 1
                        ,shopList.getGoodsName(),shopList.getTenantId(),shopList.getShopsTypeId(),shopList.getIsSpec(),shopList.getStatus(),shopList.getGoodsSkuId());
                        break;
                    case R.id.tv_cart_goodsName:
                    case R.id.rv_cart_goosImg:
                        cartFragment.toGoodDetils(shopList.getGoodsId(),shopList.getShopsTypeId());
                        break;
                    case R.id.rl_check:
                    case R.id.cb_cart_seller_item:
                        cartFragment.checkManagerItem(shopList.getTenantId(),shopList.getGoodsId());

                        break;
                    case R.id.tv_cart_settleaccounts:

                        break;
                    case R.id.tv_delete:
                         cartFragment.cartRemove(shopList.getId());
                        break;
                }
            }
        });

    }


}
